import { GridApi } from 'ag-grid-community';
import dayjs from 'dayjs';
import { useEmitter } from 'src/emitter';
import { Ref } from 'vue';

/**
 * 表格数据导出
 */
export const useExport = <TData = any>(gridApi: Ref<GridApi<TData> | undefined>, options: { id?: string } = {}) => {
  useEmitter('table_export', payload => {
    if (payload.id === options.id) {
      const params = {
        fileName: `${payload.title}${dayjs().format('YYYY-MM-DD HH-mm-ss')}`,
      };
      gridApi.value?.exportDataAsCsv(params);
    }
  });
};
